﻿using Dapper;
using Dapper.Contrib.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoFlatnessTest.Db
{
    /// <summary>
    /// 项目
    /// </summary>
    public class ProjectDb
    {
        public static Project Get(string id)
        {
            using (var connection = DbHelp.GetSQLiteConn())
            {
                return connection.Get<Project>(id);
            }
        }

        /// <summary>
        /// 得到所有的名称
        /// </summary>
        /// <param name="rackCode"></param>
        public static List<ProjectNameDto> GetNamsAll(bool homeShow = false)
        {
            using (var connection = DbHelp.GetSQLiteConn())
            {
                if (homeShow)
                    return connection.GetAll<ProjectNameDto>().Where(o => o.HomeShow == 1).OrderBy(o => o.Sort).ToList();
                else
                    return connection.GetAll<ProjectNameDto>().OrderBy(o => o.Sort).ToList();
            }
        }

        public static bool Add(string name)
        {
            using (var connection = DbHelp.GetSQLiteConn())
            {
                ProjectAddDto projectAddDto = new ProjectAddDto();
                projectAddDto.Id = DbHelp.ExplicitKey;
                projectAddDto.Name = name;
                projectAddDto.CreatedTime = DateTime.Now;

                connection.Insert(projectAddDto);
                return true;
            }
        }

        public static bool Updata(ProjectUpdataDto dto)
        {
            using (var connection = DbHelp.GetSQLiteConn())
            {
                dto.UpdatedTime = DateTime.Now;

                connection.Update<ProjectUpdataDto>(dto);
                return true;
            }
        }

        public static bool UpdataName(ProjectUpdataNameDto dto)
        {
            using (var connection = DbHelp.GetSQLiteConn())
            {
                dto.UpdatedTime = DateTime.Now;

                connection.Update<ProjectUpdataNameDto>(dto);
                return true;
            }
        }

        public static bool Delect(string id)
        {
            using (var connection = DbHelp.GetSQLiteConn())
            {
                ProjectDelectDto projectDelectDto = new ProjectDelectDto()
                {
                    Id = id,
                };

                connection.Delete(projectDelectDto);
                return true;
            }

        }


    }
}
